package ian.swing;

import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

import org.jbpm.api.TaskService;

public class FormFrame extends JFrame implements MouseListener{
	
	private MyProcessEngine mpe = MyProcessEngine.getProcessEngineInstance();
	private String taskId = "";
	private JTextField dayTF = new JTextField();
	private JTextField reasonTF = new JTextField();
	private JButton submitBtn = new JButton("Submit");
	private JButton approveBtn = new JButton("Approve");
	private JButton rejectBtn = new JButton("Reject");
	
	public FormFrame(String taskId, boolean isManager){
		super();
		this.taskId = taskId;
		this.setLayout(new GridLayout(2,3));
		this.add(dayTF);
		this.add(reasonTF);
		this.add(submitBtn);
		this.add(approveBtn);
		this.add(rejectBtn);
		this.submitBtn.addMouseListener(this);
		this.approveBtn.addMouseListener(this);
		this.rejectBtn.addMouseListener(this);
		if(isManager){
			submitBtn.setVisible(false);
			this.dayTF.setText(mpe.getTaskVar(taskId, "dayOff"));
			this.reasonTF.setText(mpe.getTaskVar(taskId, "reason"));
		}
		else{
			approveBtn.setVisible(false);
			rejectBtn.setVisible(false);
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		if(e.getComponent() == submitBtn){
			mpe.submitTask(taskId, new Integer(dayTF.getText()), reasonTF.getText());
			this.dispose();
		}
		else if(e.getComponent() == approveBtn){
			mpe.examineTask(true, taskId);
			this.dispose();
		}
		else{
			mpe.examineTask(false, taskId);
			this.dispose();
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}
